Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HCPE-842: bubble up consul service errors #4

Merged
merged 4 commits into from
Feb 10, 2021

Conversation

bcmdarroch
Copy link
Contributor

@bcmdarroch bcmdarroch commented Feb 9, 2021

🛠️ Description

This PR regenerates the consul service SDK from swagger that was regenerated using grpc-gateway v1.15. This version of grpc-gateway contains this fix, which adds a default error response to each endpoint.

Due to another change that added a ConsulService_ prefix to each endpoint to ensure uniqueness (an OpenAPI requirement), all of the file names were changed.

🏞 Background

Currently, all the service swagger files lack a default error object. This means go-swagger can't generate an error handling function for the SDK's consul client, and so the consul client can only return the status code and this unhelpful message:

Error: unable to get Consul cluster (cluster1): response status code does not match any response statuses defined for this endpoint in the swagger spec (status 404): {}

With this change, go-swagger will generate error handlers from the default error response in the swagger. Specifically, we get this block:

// Code gets the status code for the consul service get default response
func (o *ConsulServiceGetDefault) Code() int {
	return o._statusCode
}

func (o *ConsulServiceGetDefault) Error() string {
	return fmt.Sprintf("[GET /consul/2020-08-26/organizations/{location.organization_id}/projects/{location.project_id}/clusters/{id}][%d] ConsulService_Get default  %+v", o._statusCode, o.Payload)
}

func (o *ConsulServiceGetDefault) GetPayload() *models.GrpcGatewayRuntimeError {
	return o.Payload
}

func (o *ConsulServiceGetDefault) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {
	o.Payload = new(models.GrpcGatewayRuntimeError)

	// response payload
	if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF {
		return err
	}
	return nil
}

These handlers pass along the complete API error:

[GET /consul/2020-08-26/organizations/{location.organization_id}/projects/{location.project_id}/clusters/{id}][404] ConsulService_Get default  &{Code:5 Details:[] Error:consul cluster not found Message:consul cluster not found}

🔗 External Links

Follow-up to: https://github.com/hashicorp/cloud-consul-service/pull/964 and https://github.com/hashicorp/cloud-api/pull/32
HCPE-791

🏗️ How to Build and Test the Change

  1. With your local environment running, set the following environment variables:
export HCP_CLIENT_ID="servicePrincipalKeyClientID"
export HCP_CLIENT_SECRET="servicePrincipalKeyClientSecret"
export HCP_API_HOST="some.host.com"        (if unset, uses prod default)
export HCP_AUTH_URL="some.host.com"        (if unset, uses prod default)
  1. Update /cmd/hcp-sdk-go-client/main.go with some call that will error (like a GET for a consul cluster that doesn't exist).
getParams := consul.NewConsulServiceGetParams()
getParams.LocationOrganizationID = orgID
getParams.LocationProjectID = projID
getParams.ID := "not-found"

resp3, err := consulClient.ConsulServiceGet(getParams, nil)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Response: %#v\n\n", resp3.Payload)
  1. go run ./cmd/hcp-sdk-go-client. Check out that complete API error! ✨

👍 Definition of Done

  • consul service errors are returned with complete details

from swagger regenerated using grpc-gateway 1.15
to get default error response
also adds Consul_Service to operationIDs to ensure uniqueness,
resulting in renamed files and functions
@bcmdarroch bcmdarroch self-assigned this Feb 10, 2021
to force run of newly setup CI
@bcmdarroch bcmdarroch merged commit 4211074 into main Feb 10, 2021
@bcmdarroch bcmdarroch deleted the hcpe-791/update-consul-sdk-errors branch February 10, 2021 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants